<?php

require_once APPPATH."/controllers/BasePriv.php";
require_once APPPATH."/libraries/aws/aws-autoloader.php";
require_once APPPATH."/libraries/Tools.php";
require  APPPATH."/libraries/Uploads.php";
require  APPPATH."/libraries/upload/SingleUpload.php";
use Aws\S3\S3Client;
class Upload extends BasePriv
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', "url"));
    }

    /**
     * 这是上传普通文件的
     * audio   音频
     * image   图片
     * video   视频
     */
    public function normal_upload(){
        $type=$_REQUEST['type'];
        if(empty($type)){
            $type="test";
        }
//        $upload_type=$_FILES['file']['type'];
//        $checked=$this->check_type($type,$upload_type);
//        $checked=true;
//        if($checked){
        //不能上传空文件
        if(empty($_FILES["file"]) || !file_exists($_FILES["file"]["tmp_name"])){
            response(-1,"文件不能为空");
        }
        try{
            //解决跨域问题
            $this->config->load("upload");
            $uploadConfigs = config_item("upload");
            //创建上传目录，配置上传
            $date1=date("Y",time());
            $date2=date("m",time());
            $date3=date("d",time());
            $dir="/".$date1."/".$date2."/".$date3;
            $uploadpath1=$uploadConfigs[$type]['uploadpath'];
            $tmppath1=$uploadConfigs[$type]['tmppath'];
            $uploadpath=$uploadpath1.$dir;
            $tmppath=$tmppath1.$dir;
            $config['filename']=$uploadConfigs[$type]['filename'];
            $config['uploadpath']=$uploadpath;
            $config['tmppath']=$tmppath;
            $config['allowtype']=$uploadConfigs[$type]['allowtype'];
            $config['types']=$type;
            $config['savehttp']=$uploadConfigs[$type]['savehttp'].$dir;
            //开启上传
            $singleUpload=new Uploads($config);
            $da=$singleUpload->doupload();
            $fileInfo['file_name'] = $singleUpload->getFinalFileName();
            $fileInfo['file_url'] = $singleUpload->savehttp();
            $move_tmp_path=$tmppath."/".$fileInfo['file_name'];
            $cmd1="chmod 777 {$move_tmp_path}";
            $cmd2="chmod 777 {$uploadpath}";
            $cmd3="mv {$move_tmp_path} {$uploadpath}";

            exec($cmd1);
            exec($cmd2);
            exec($cmd3);


            //上传成功
            $this->output(0,"ok",$fileInfo);
        }catch (Exception $e){
            $this->output(-1,"上传失败msg==>".$e->getMessage());
        }

//        }else{
//            $this->output(-1,"暂时不支持该类型上传");
//        }
    }

    /**
     * 检验上传的类型
     */
    public function check_type($type,$upload_type){
        //视频
//        ,accept: 'video'
//        ,acceptMime: 'video/mp4,video/mov,video/avi'
        //音频
//        ,accept:'audio'
//        ,acceptMime: 'audio/mp3,audio/aac'
        //图片
//        ,acceptMime: 'image/*'



        //允许上传类型
        $video=["video/mp4","video/mov","video/avi",""];
        $image=["image/png","image/jpeg","image/jpg","image/gif",""];
        $audio=["audio/mp3","audio/aac",""];
        //检测上传类型
        if($type=="image"){
            if(in_array($upload_type,$image)){
                return true;
            }else{
                return false;
            }
        }else if($type=="audio"){
            if(in_array($upload_type,$audio)){
                return true;
            }else{
                return false;
            }
        }else if($type=="video"){
            if(in_array($upload_type,$video)){
                return true;
            }else{
                return false;
            }
        }else if($type=="test"){

            return true;

        } else{
            return false;
        }
    }
}